/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import share.PGHelper;

/**
 *
 * @author KieuAnh
 */
public class CFAchievements extends JSONMapString<CFAchievements.Achievement>
{
    private CFAchievements() {}
    public static CFAchievements parse(Map<String, Object> json)
    {
        CFAchievements achivements = new CFAchievements();
        achivements.deser(json);
        
        return achivements;
    }

    @Override
    protected Achievement newElement(Map<String, Object> elemJSON) {
        return new Achievement();
    }

    @Override
    public void deser(Map<String, Object> json) {
        super.deser((Map) json.get("data"));
    }
    
    public static class Achievement extends JSONMapString<Achievement.Medal>
    {
        private String action;
        private boolean enable;
        
        private final Map<Integer, List<String>> stars = new HashMap();

        @Override
        protected Medal newElement(Map<String, Object> elemJSON) {
            return new Medal();
        }

        @Override
        public void deser(Map<String, Object> json) {
            this.action = (String) json.get("action");
            this.enable = (Boolean) json.get("enable");
            this.deserStars((Map) json.get("stars"));
            super.deser((Map) json.get("medals"));
        }
        
        private void deserStars(Map<String, Object> json)
        {
            for (Map.Entry<String, Object> starEntry : json.entrySet()) {
                int star = PGHelper.toInt(starEntry.getKey());
                Map<String, Object> starData = (Map) starEntry.getValue();
                Map<String, String> medals = (Map) starData.get("medals");
                
                List<String> starMedals = new ArrayList(medals.size());
                for (int i = 0; i < medals.size(); ++i)
                {
                    starMedals.add(null);
                }
                
                for (Map.Entry<String, String> starMedalsEntry : medals.entrySet()) {
                    Integer index = PGHelper.toInt(starMedalsEntry.getKey());
                    String medal = starMedalsEntry.getValue();
                    
                    starMedals.set(index, medal);
                }
                
                stars.put(star, starMedals);
            }
        }

        public String getAction() {
            return action;
        }

        public boolean isEnable() {
            return enable;
        }
        
        public String getMaxMedal(int star)
        {
            return stars.get(star).get(stars.get(star).size() - 1);
        }
        
        public Map<Integer, List<String>> stars()
        {
            return this.stars;
        }
        
        public static class Medal implements JSONable
        {
            private Object require;
            private Map<String, Object> prize;

            private Medal() {}

            @Override
            public void deser(Map<String, Object> json) {
                this.require = json.get("require");
                this.prize = (Map) json.get("prize");
            }  

            public Object getRequire() {
                return require;
            }

            public Map<String, Object> getPrize() {
                return prize;
            }
        }
    }
}
